package com.shanwen.shopping.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.DialogInterface;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.shanwen.shopping.R;
import com.shanwen.shopping.consts.Consts;
import com.shanwen.shopping.http.ActionHelp;
import com.shanwen.shopping.view.QuantityView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by classic on 16/4/6.
 */
public final class Utils {
    private Utils(){}
    
    public static final void showToast(Context context, String content){
        Toast toast = Toast.makeText(context.getApplicationContext(), content, Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.TOP|Gravity.CENTER_HORIZONTAL, 0, 380);
        toast.show();
    }

    public static String getPrice(long price){
        double value = MoneyUtil.newInstance(price).divide(100).create().doubleValue();
        //double value = MoneyUtil.divide(price,100).doubleValue();
        return MoneyUtil.replace(value);
    }
    public static String formatPrice(long price){
        double value = MoneyUtil.newInstance(price).divide(100).create().doubleValue();
        //double value = MoneyUtil.divide(price,100).doubleValue();
        return new StringBuilder(Consts.MONEY_PREFIX).append(MoneyUtil.replace(value)).toString();
    }

    /**
     * 获取json格式后的订单对象数据信息
     * @param addressJson json格式化的收获地址信息
     * @param goodsListJson json格式后的商品列表信息
     * @param remark <可选参数>订单备注信息
     *               String phone;//手机号
        String prisonerCode;//犯人编号
     * @return
     */
    public static String getonfirmOrderJson(JSONObject addressJson,JSONArray goodsListJson,String phone,String prisonerCode,String remark){
        JSONObject obj = new JSONObject();
        try {
//            obj.put("delivelyTimeType",1); //1==全天；2==工作日；3==假日，默认工作日
//            obj.put("expressMoney",0);
            obj.put("orderType",1);
//            obj.put("deliveryAddress",addressJson);
            obj.put("phone",phone);
            obj.put("prisonerCode",prisonerCode);
            obj.put("goodsList",goodsListJson);
            if(!TextUtils.isEmpty(remark)){
                obj.put("remark",remark);
            }
        } catch (JSONException e) {
            e.printStackTrace();
            return "";
        }
        return obj.toString();
    }

    /**
     * 获取单个商品json格式后的商品信息
     * @param goodsId 商品主键编号
     * @param buyNum 商品购买数量
     * @param title 商品标题数组
     * @param coverUrl 商品封面url
     * @param skuProp 用户购买商品的最终sku属性
     * @param skuId 商品sku的主键编号
     * @return
     */
    public static JSONObject getGoodsListJson(long goodsId,long buyNum,String title,String coverUrl,
                                          JSONObject skuProp,long skuId){
        JSONObject obj = new JSONObject();
        try {
            obj.put("goodsId",goodsId);
            obj.put("buyNum",buyNum);
            obj.put("title",title);
            obj.put("coverUrl",coverUrl);
            obj.put("skuProp",skuProp);
            obj.put("skuId",skuId);
            obj.put("activityId",0);
//            obj.put("shoppingCartId",0);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return obj;
    }

    /*
    "addressId": 12, //主键编号
            "isDefault": true,// 是否默认地址 true=是 false=否 boolean类型
            "phone":"15715766666",//收货人电话
            "name": "马云",//收货人姓名
            "detailAddress": "文一路115号",//收货人详细地址
            "zipCode": "310000",//邮编
            "provinceName": "浙江",//省份名称
            	   "cityName": "杭州"//地级市名称
"areaName ":"西湖"//县区名称
"areaAddressId ":3299//县区名称主键id
"proviceId ":30,//省份主键id，int类型
"cityId ":382,//市主键id，int类型
     */

    /**
     * 获取json格式化的收获地址信息
     * @param name 姓名
     * @param address 地址
     * @param code 邮编
     * @return
     */
    public static JSONObject getAddressJson(String name,String address, String code){
        JSONObject obj = new JSONObject();
        try {
            obj.put("name",name);
            obj.put("detailAddress",address);
            obj.put("zipCode",code);
            obj.put("phone", ActionHelp.ACCOUNT);
            obj.put("isDefault",true);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return obj;
    }

    public static JSONObject getSkuProp(String skuPropName,String skuPropValue){
        final JSONObject obj = new JSONObject();
        try {
            obj.put(skuPropName,skuPropValue);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return obj;
    }


    public static void setQuantityClick(final Context context,final QuantityView quantityView){
        quantityView.setQuantityClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(context);
                builder.setTitle("修改数量");

                View inflate = LayoutInflater.from(context).inflate(R.layout.dialog_changequantity, null, false);
                final EditText et = (EditText) inflate.findViewById(R.id.dialog_quantity_et);
                et.setText(String.valueOf(quantityView.getQuantity()));
                et.setSelection(et.getText().length());
//                et.addTextChangedListener(new TextWatcher() {
//                    @Override
//                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//                    }
//
//                    @Override
//                    public void onTextChanged(CharSequence s, int start, int before, int count) {
//                        if (TextUtils.isEmpty(s)) return;
//                        int intNewQuantity = Integer.parseInt(s.toString());
//                    }
//
//                    @Override
//                    public void afterTextChanged(Editable s) {
//
//                    }
//                });

                builder.setView(inflate);
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String newQuantity = et.getText().toString();
                        if (TextUtils.isEmpty(newQuantity)) return;
                        int intNewQuantity = Integer.parseInt(newQuantity);
                        quantityView.setQuantity(intNewQuantity);
                    }
                }).setNegativeButton("取消", null);
                builder.show();
            }
        });

    }

    @SuppressLint("StaticFieldLeak")
    private static Context context;

    /**
     * 初始化工具类
     *
     * @param context 上下文
     */
    public static void init(@NonNull final Context context) {
        Utils.context = context.getApplicationContext();
    }

    /**
     * 获取ApplicationContext
     *
     * @return ApplicationContext
     */
    public static Context getContext() {
        if (context != null) return context;
        throw new NullPointerException("u should init first");
    }
}
